package net.sample.gismeteo.gui;

import java.security.InvalidParameterException;
import java.util.List;

import net.sample.gismeteo.Common;
import net.sample.gismeteo.R;
import net.sample.gismeteo.WeatherInfo;
import net.sample.gismeteo.net.WeatherXMLParser;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

/**
 * @author vkozlov
 * @version $Id: $
 */
public class ShowWeatherForecastActivity extends Activity
{

	private static final String TAG = "ShowWeatherForecastActivity";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		//setTheme(android.R.style.Theme_Light);
		setContentView(R.layout.forecast);
		if (!displayForecast(getIntent().getData()))
		{
			//finish();
		}
	}

	protected boolean displayForecast(Uri pUri)
	{
		try
		{
			ContentResolver lResolver = getContentResolver();
			if (null == lResolver)
			{
				throw new NullPointerException("Returned resolver is null!");
			}
			Cursor lCursor = lResolver.query(pUri, Common.PROJECTION, null, null, null);
			if (null == lCursor)
			{
				throw new NullPointerException("Returned cursor is null!");
			}
			if (lCursor.getCount() < 1)
			{
				throw new InvalidParameterException("No data returned!");
			}
			try
			{
				lCursor.moveToFirst();
				return displayForecast(lCursor.getString(lCursor.getColumnIndexOrThrow(Common.COL_BODY)));
			}
			finally
			{
				lCursor.close();
			}
		}
		catch (Exception lExcept)
		{
			Log.e(TAG, "can not display forecast", lExcept);
			new AlertDialog.Builder(this)
				.setMessage(String.format("Can not display forecast: %1$s!", lExcept.getMessage()))
				.setTitle("ARRRRGH!!!!!")
				.setPositiveButton("Close", new DialogInterface.OnClickListener()
				{
					public void onClick(DialogInterface pDialog, int pWhich)
					{
						ShowWeatherForecastActivity.this.finish();
					}})
				.show();
			return false;
		}
	}

	protected boolean displayForecast(String pXML)
	{
		WeatherXMLParser parser = new WeatherXMLParser();
		try
		{
			List<WeatherInfo> list = parser.parse(pXML);
			// for (WeatherInfo info : list)
			// {
			// Log.d(TAG, info.toString());
			// Log.d(TAG, info.getWeatherImageResourceName());
			// }
			ListView listView = (ListView) findViewById(R.id.ListView01);
			listView.setAdapter(new WeatherDataListAdapter(this, list, listView));
		}
		catch (Exception lExcept)
		{
			Log.e(TAG, "unable to display the forecast", lExcept);
			new AlertDialog.Builder(this)
				.setMessage("Can not display forecast: some GUI failure!")
				.setTitle("ARRRRGH!!!!! v2")
				.setPositiveButton("Close", new DialogInterface.OnClickListener()
				{
					public void onClick(DialogInterface pDialog, int pWhich)
					{
						ShowWeatherForecastActivity.this.finish();
					}})
					.show();
			return false;
		}
		return true;
	}
}
